<template>
  <li class="chat-message-item" :class="{ right, bigger }">
    <div class="avatar">
      <img class="avatar-img" :src="avatarUrl" :alt="realname">
    </div>
    <div class="main">
      <p v-if="realname !== null" class="realname">
        {{ realname }}
      </p>
      <div class="cloud">
        <slot name="default">
          <p> </p>
        </slot>
        <img v-if="right" src="@/assets/triangle_right.svg" alt="" class="triangle-right">
        <img v-else src="@/assets/triangle_left.svg" alt="" class="triangle-left">
      </div>
    </div>
  </li>
</template>

<script>
import defaultAvatar from '@/assets/default_avatar.jpg'

export default {
  name: 'ChatMessageItem',
  props: {
    right: {
      type: Boolean,
      default: false
    },
    avatar: {
      default: null,
      validator(value) {
        return (typeof value === 'string') || (value === null)
      }
    },
    realname: {
      default: null,
      validator(value) {
        return (typeof value === 'string') || (value === null)
      }
    },
    bigger: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    avatarUrl() {
      return this.avatar || defaultAvatar
    }
  }
}
</script>

<style scoped>
.chat-message-item {
  display: flex;
  box-sizing: border-box;
  padding: 9px 12px;
  flex-direction: row;
  align-items: flex-start;
  width: 100%;
}
.chat-message-item.right {
  flex-direction: row-reverse;
}
.avatar {
  flex: 0 0 40px;
  height: 40px;
  width: 40px;
  border-radius: 50%;
  background: #e5e5e5;
  user-select: none;
}
.chat-message-item.bigger .avatar {
  flex: 0 0 60px;
  height: 60px;
  width: 60px;
}
.avatar-img {
  display: block;
  height: 100%;
  width: 100%;
  border-radius: 50%;
  object-fit: cover;
}
.realname {
  margin: 2px auto 2px 12px;
  font-size: 11px;
  color: #999;
}
.chat-message-item.bigger .realname {
  margin: 4px auto 4px 12px;
  font-size: 12px;
}
.cloud {
  position: relative;
  margin-left: 12px;
  margin-right: 30px;
  box-sizing: border-box;
  padding: 12.33px;
  border-radius: 15px;
  color: #000;
  background: #fff;
  font-size: 10px;
}
.chat-message-item.bigger .cloud {
  font-size: 14px;
}
.chat-message-item.right .cloud {
  margin-left: 30px;
  margin-right: 12px;
  background-color: #1fbafc;
  color: #fff;
}
.triangle-left {
  position: absolute;
  height: 14px;
  width: 7px;
  top: 10px;
  left: -6px;
}
.triangle-right {
  position: absolute;
  height: 14px;
  width: 7px;
  top: 10px;
  right: -6px;
  transform: rotate(180deg);
}
.triangle-left,
.triangle-right {
  user-select: none;
}
</style>
